package com.xspace.android.xafcommon.util;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

import com.xspace.android.xafcommon.appliction.BaseApplication;


//Toast统一管理类
public class T {


    private static Toast sToast;

    private T() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    private static Handler mHandle = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            showSingle( msg.getData().get("key").toString(), Toast.LENGTH_SHORT);
//            Toast.makeText(mContext, msg.getData().get("key").toString(), Toast.LENGTH_SHORT).show();
        }
    };
    public static boolean isShow = true;

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showShort(final Context context, final CharSequence message) {
        if (isShow) {
            Message sendMsg = mHandle.obtainMessage();
            Bundle dataBundle = new Bundle();
            dataBundle.putString("key", message.toString());
            sendMsg.setData(dataBundle);
            mHandle.sendMessage(sendMsg);
        }
//        mHandle.sendEmptyMessage(0);
//        if (context instanceof Activity) {
//            ((Activity) context).runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
//                }
//            });
//        }

    }

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showShort(final Context context, final int message) {
        if (isShow)
            if (context instanceof Activity) {
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                    }
                });
            }

    }

    /**
     * 长时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showLong(Context context, CharSequence message) {
        if (isShow)
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    /**
     * 长时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showLong(Context context, int message) {
        if (isShow)
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param message
     * @param duration
     */
    public static void show(Context context, CharSequence message, int duration) {
        if (isShow)
            Toast.makeText(context, message, duration).show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param message
     * @param duration
     */
    public static void show(Context context, int message, int duration) {
        if (isShow)
            Toast.makeText(context, message, duration).show();
    }

    /**
     * 显示一个指定时长的单例吐司
     *
     * @param msg      消息
     * @param duration 时长
     */
    public static void showSingle(String msg, int duration) {
        if (sToast == null) {
            sToast = Toast.makeText(BaseApplication.mInstance, msg, duration);
        }
        sToast.setText(msg);
        sToast.setDuration(duration);
        sToast.show();
    }

    /**
     * 输出一个单例吐司,显示较短时长
     *
     * @param text 要显示的文本
     */
    public static void showSingleL(String text) {
        showSingle(text, Toast.LENGTH_SHORT);
    }

}